<template>
  <div class="search">
    <Row>
      <Col>
        <Card>
          <Row class="operation">
            <Row class="operation">
              <Button @click="getDataList" icon="md-refresh">刷新</Button>
              <Button @click="add" type="primary" icon="md-add" v-if="data.length == 0">添加</Button>
            </Row>
          </Row>
          <Row>
            <Table :loading="loading" border :columns="columns" :data="data" ref="table" sortable="custom" @on-sort-change="changeSort"></Table>
          </Row>
          <Row type="flex" justify="end" class="page">
            <Page :current="searchForm.pageNumber" :total="total" :page-size="searchForm.pageSize" @on-change="changePage" @on-page-size-change="changePageSize" :page-size-opts="[10,20,50]" size="small" show-total show-elevator show-sizer></Page>
          </Row>
        </Card>
      </Col>
    </Row>
    <Modal :title="modalTitle" v-model="modalVisible" :mask-closable='false' :width="700">
      <Form ref="form" :model="form" :label-width="100" :rules="formValidate" >
        <FormItem label="新版本号" prop="newVersion">
          <Input v-model="form.newVersion" placeholder="请输入新版本号" />
        </FormItem>
        <FormItem label="旧版本号" prop="minVersion">
          <Input v-model="form.minVersion" placeholder="请输入旧版本号" />
        </FormItem>
        <FormItem label="旧版本号" prop="apkUrl">
          <upload-apk-input v-model="form.apkUrl" style="width: 100%"></upload-apk-input>
        </FormItem>
        <FormItem label="更新文案" prop="updateDescription">
          <Input v-model="form.updateDescription" placeholder="请输入更新文案" type="textarea" />
        </FormItem>
        <FormItem label="是否有更新" prop="isUpdate">
          <Select v-model="form.isUpdate" style="width: 100%">
            <Option value=true>是</Option>
            <Option value=false>否</Option>
          </Select>
        </FormItem>
        <FormItem label="是否强制更新" prop="forceUpdate">
          <Select v-model="form.forceUpdate" style="width: 100%">
            <Option value=true>是</Option>
            <Option value=false>否</Option>
          </Select>
        </FormItem>
      </Form>
      <div slot="footer">
        <Button type="text" @click="modalVisible=false">取消</Button>
        <Button type="primary" :loading="submitLoading" @click="handleSubmit">提交</Button>
      </div>
    </Modal>
    
  </div>
</template>

<script>
import uploadApkInput from "@/views/my-components/xboot/upload-apk-input"
import circleLoading from "@/views/my-components/circle-loading.vue";
import uploadPicInput from "@/views/my-components/xboot/upload-pic-input.vue"
export default {
  name: "staffManagement",
  components: {
    circleLoading,
    uploadPicInput,
    uploadApkInput
  },
  data() {
    return {
      loading: true, // 表单加载状态
      modalType: 0, // 添加或编辑标识
      modalVisible: false, // 添加或编辑显示
      modalTitle: "", // 添加或编辑标题
      searchForm: { // 搜索框初始化对象
        currentVersion: ""
      },
      form: { // 添加或编辑表单对象初始化数据
        id: "",
        newVersion:'',
        minVersion:'',
        apkUrl:'',
        updateDescription:'',
        isUpdate:'',
        forceUpdate:'',
      },
      // 表单验证规则
      formValidate: {
        newVersion: [{ required: true, message: "请输入新的版本号", trigger: "blur" }],
        minVersion: [{ required: true, message: "强输入", trigger: "blur" }],
        apkUrl: [{ required: true, message: "请选择下载app", trigger: "change" }],
        updateDescription: [{ required: true, message: "请输入更新文案", trigger: "blur" }],
        isUpdate: [{ required: true, message: "请选择是否有更新", trigger: "change" }],
        forceUpdate: [{ required: true, message: "请选择是否强制更新", trigger: "change" }],
      },
      submitLoading: false, // 添加或编辑提交状态
      columns: [
        // 表头
        {
          type: "index",
          width: 60,
          align: "center"
        },
        {
          title: "新版本",
          key: "newVersion",
          minWidth: 120,
          sortable: false,
        },
        {
          title: "旧版本",
          key: "minVersion",
          minWidth: 120,
          sortable: false,
        },
        {
          title: "下载地址",
          key: "apkUrl",
          minWidth: 120,
          sortable: false,
        },
        {
          title: "修改文案",
          key: "updateDescription",
          minWidth: 120,
          sortable: false,
        },
        {
          title: "是否需要更新",
          key: "isUpdate",
          minWidth: 120,
          sortable: false,
          render: (h,params) => {
            if (params.row.isUpdate) {
              return h("span","需要")
            } else if (!params.row.isUpdate) {
              return h("span","不需要")
            }
          }
        },
        {
          title: "是否强制更新",
          key: "forceUpdate",
          minWidth: 120,
          sortable: false,
          render: (h,params) => {
            if (params.row.forceUpdate) {
              return h("span","强制")
            } else if (!params.row.forceUpdate) {
              return h("span","不强制")
            }
          }
        },
        {
          title: "操作",
          key: "action",
          align: "center",
          fixed: "right",
          minWidth: 200,
          render: (h, params) => {
            return h("div", [
              h(
                "Button",
                {
                  props: {
                    type: "primary",
                    size: "small"
                  },
                  style: {
                    marginRight: "5px"
                  },
                  on: {
                    click: () => {
                      this.edit(params.row);
                    }
                  }
                },
                "编辑"
              ),
            ]);
          }
        }
      ],
      data: [], // 表单数据
      total: 0 // 表单数据总数
    };
  },
  methods: {
    init() {
      this.getDataList();
    },
    changePage(v) {
      this.searchForm.pageNumber = v;
      this.getDataList();
    },
    changePageSize(v) {
      this.searchForm.pageSize = v;
      this.getDataList();
    },
    handleReset() {
      this.$refs.searchForm.resetFields();
      this.searchForm.pageNumber = 1;
      this.searchForm.pageSize = 10;
      // 重新加载数据
      this.getDataList();
    },
    changeSort(e) {
      this.searchForm.sort = e.key;
      this.searchForm.order = e.order;
      if (e.order === "normal") {
        this.searchForm.order = "";
      }
      this.getDataList();
    },
    getDataList() {
        this.loading = true;
        // 带多条件搜索参数获取表单数据 请自行修改接口
        this.postRequest("/versions/getVersions", this.searchForm).then(res => {
            this.loading = false;
            this.data = [];
            if (res.success) {
              this.data.push(res.result);
              this.total = res.result.totalSize;
            } else if (res.code == 201) {
              this.data=[];
              this.total=0;
            }
        });
    },
    handleSubmit() {
        this.$refs.form.validate(valid=> {
            if (valid) {
                this.submitLoading = true;
                if (this.modalType === 0) {
                    delete this.form.id;
                    this.postRequest("/versions/insertOrderUpdateVersions", this.form).then(res=> {
                        this.submitLoading = false;
                        if (res.success) {
                            this.$Message.success("操作成功");
                            this.getDataList();
                            this.modalVisible = false;
                        } else {
                            this.$Message.success(res.message);
                            this.modalVisible = false;
                        }
                    })
                } else {
                    this.postRequest("/versions/insertOrderUpdateVersions", this.form).then(res=> {
                        this.submitLoading = false;
                        if (res.success) {
                            this.$Message.success("操作成功");
                            this.getDataList();
                            this.modalVisible = false;
                        } else {
                            this.$Message.success(res.message);
                            this.modalVisible = false;
                        }
                    })
                }
            }
        })
    },
    add () {
        this.modalType = 0;
        this.modalTitle = "添加";
        this.$refs.form.resetFields();
        this.modalVisible = true;
    },
    edit (v) {
        this.form.id = v.id;
        this.form.newVersion = v.newVersion;
        this.form.minVersion = v.minVersion;
        this.form.apkUrl = v.apkUrl;
        this.form.updateDescription = v.updateDescription;
        v.isUpdate ? this.form.isUpdate = "1" : this.form.isUpdate = "0"
        v.forceUpdate ? this.form.forceUpdate = "1" : this.form.forceUpdate = "0"
        this.modalType = 1;
        this.modalTitle = "编辑";
        this.modalVisible = true;
    },
},
  mounted() {
    this.init();
  }
};
</script>
<style lang="less">
.search {
    .operation {
        margin-bottom: 2vh;
    }
    .select-count {
        font-size: 13px;
        font-weight: 600;  
        color: #40a9ff;
    }
    .select-clear {
        margin-left: 10px;
    }
    .page {
        margin-top: 2vh;
    }
    .drop-down {
        font-size: 13px;
        margin-left: 5px;
    }
}
</style>